package com.kye.express.business.deposit.presenter;


import com.kye.express.business.deposit.fragment.DepositRechargeFragment;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.deposit.RequestShowPaymentRecord;
import com.kye.model.bean.deposit.ResultShowPaymentRecordBean;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.DepositDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 预存运费-充值记录 Presenter
 * <p>
 * Created by SkyYu on 2018/1/17
 */
public interface DepositRechargeContract {

    interface DepositRechargeView extends IView {
        void onLoadUserData(User user);

        void onLoading();

        void onComplete(boolean isLoadMore);

        void onSuccessLoadData(OpenApiResult<ResultShowPaymentRecordBean> bean, boolean isFirstIn, boolean isLoadMore);

        void onApiError(OpenApiResult<ResultShowPaymentRecordBean> result, int code, String msg);

        void onOtherError(Throwable e);
    }

    class DepositRechargePresenter extends BasePresenter<DepositRechargeView> {

        DepositDataSource dataSource;
        DepositRechargeFragment mCurrentFragment;

        public DepositRechargePresenter(DepositRechargeFragment fragment) {
            dataSource = new DepositDataSource();
            mCurrentFragment = fragment;
        }

        @Override
        public void bind(DepositRechargeView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((user) ->
                            getView().onLoadUserData(user)
                    );
        }

        void requestToShowPaymentRecord(boolean isAutoLoading, boolean isFirstIn, boolean isLoadMore, String phone, int pageIndex, int pageSize) {
            dataSource.showPaymentRecord(new RequestShowPaymentRecord(phone, isLoadMore ? (pageIndex + 1) : 1, pageSize))
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResultShowPaymentRecordBean>(this) {
                        @Override
                        public void onStart(Disposable d) {
                            if (isAutoLoading) {
                                getView().onLoading();
                            }
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete(isLoadMore);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResultShowPaymentRecordBean> result, ResultShowPaymentRecordBean record) {
                            if (!isLoadMore) {
                                resetPageIndex();
                            } else {
                                setCurrentPageIndex(mCurrentFragment.mCurrentPageIndex + 1);
                            }
                            getView().onSuccessLoadData(result, isFirstIn, isLoadMore);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResultShowPaymentRecordBean> result, int code, String msg) {
                            getView().onApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(e);
                        }
                    });
        }

        public void refreshData(boolean isAutoLoading, boolean isFirstIn, boolean isLoadMore, String phone, int pageIndex, int pageSize) {
            requestToShowPaymentRecord(isAutoLoading, isFirstIn, isLoadMore, phone, pageIndex, pageSize);
        }

        public void loadMore(boolean isAutoLoading, boolean isFirstIn, boolean isLoadMore, String phone, int pageIndex, int pageSize) {
            requestToShowPaymentRecord(isAutoLoading, isFirstIn, isLoadMore, phone, pageIndex, pageSize);
        }

        void resetPageIndex() {
            setCurrentPageIndex(1);
        }

        void setCurrentPageIndex(int currentPage) {
            mCurrentFragment.mCurrentPageIndex = currentPage;
        }
    }
}